//
//  HUDView.swift
//  PKHUD
//
//  Created by Philip Kluz on 6/16/14.
//  Copyright (c) 2016 NSExceptional. All rights reserved.
//  Licensed under the MIT license.
//

import UIKit

/// Provides the general look and feel of the PKHUD, into which the eventual content is inserted.
internal class FrameView: UIVisualEffectView {

	internal init() {
		super.init(effect: UIBlurEffect(style: .Light))
		commonInit()
	}

	required init?(coder aDecoder: NSCoder) {
		super.init(coder: aDecoder)
		commonInit()
	}

	private func commonInit() {
		// MARK: HUD的背景颜色
		backgroundColor = UIColor(white: 0.8, alpha: 0.36)
		layer.cornerRadius = 9.0
		layer.masksToBounds = true

		contentView.addSubview(self.content)

		let offset = 20.0

		let motionEffectsX = UIInterpolatingMotionEffect(keyPath: "center.x", type: .TiltAlongHorizontalAxis)
		motionEffectsX.maximumRelativeValue = offset
		motionEffectsX.minimumRelativeValue = -offset

		let motionEffectsY = UIInterpolatingMotionEffect(keyPath: "center.y", type: .TiltAlongVerticalAxis)
		motionEffectsY.maximumRelativeValue = offset
		motionEffectsY.minimumRelativeValue = -offset

		let group = UIMotionEffectGroup()
		group.motionEffects = [motionEffectsX, motionEffectsY]

		addMotionEffect(group)
	}

	private var _content = UIView()
	internal var content: UIView {
		get {
			return _content
		}
		set {
			_content.removeFromSuperview()
			_content = newValue
			_content.alpha = 0.85
			_content.clipsToBounds = true
			_content.contentMode = .Center
			frame.size = _content.bounds.size
			addSubview(_content)
		}
	}
}
